package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.stereotype.Repository;
import pojo.User;
import dao.UserDao;

@Repository(value="userDao")
public class UserDaoImpl implements UserDao{
	@Resource(name="dataSource")
	private DataSource dataSource;

	@Override
	public User addUser(User user) {
		String sql="insert into userInfo (id,name,sex,sal,birth) values (?,?,?,?,?)";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = dataSource.getConnection();
			user.setId(UUID.randomUUID().toString());
			pst = connection.prepareStatement(sql);
			pst.setString(1, user.getId());
			pst.setString(2, user.getName());
			pst.setBoolean(3, user.isSex());
			pst.setDouble(4, user.getSal());
			pst.setDate(5, new java.sql.Date(user.getBirth().getTime()));
			m=pst.executeUpdate();
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			close(connection, pst, null);
		}
	}

	@Override
	public int deleteUser(String id) {
		String sql="delete from userInfo where id=?";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = dataSource.getConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, id);
			m=pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally{
			close(connection, pst, null);
		}
	}

	@Override
	public int updateUser(User user) {
		String sql="update userInfo set name=?,sex=?,sal=?,birth=? where id=?";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = dataSource.getConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, user.getName());
			pst.setBoolean(2, user.isSex());
			pst.setDouble(3, user.getSal());
			pst.setDate(4, new java.sql.Date(user.getBirth().getTime()));
			pst.setString(5, user.getId());
			m=pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally{
			close(connection, pst, null);
		}
	}

	@Override
	public List<User> findAll() {
		List<User> list = new ArrayList<User>();
		Connection connection = null;
		PreparedStatement pst = null;
		String sql = "select * from userInfo";
		try {
			connection = dataSource.getConnection();
			pst=connection.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				User user = new User();
				user.setId(rs.getString("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getBoolean("sex"));
				user.setSal(rs.getDouble("sal"));
				user.setBirth(rs.getDate("birth"));
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		} finally{
			close(connection, pst, null);
		}
	}
	
	private void close(Connection connection,PreparedStatement pst, ResultSet rs){
		try {
			if(rs!=null)
				rs.close();
			if(pst!=null)
				pst.close();
			if(connection!=null)
				connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
